extends Node2D

signal upgrade
signal impact

@onready var hit_box = $Area2D

var defend = 0.8
var upgrade_time = 0

var cool_down_time = 7
var cool_down_done = true
var impact_yet = false
var impact_direction = Vector2()

func _ready() -> void:
    visible = false
    upgrade.connect(_upgrade)
    impact.connect(_impact)


func _upgrade() -> void:
    match upgrade_time:
        0:
            upgrade_time += 1
            visible = true
            hit_box.add_to_group("Guardian")
        1:
            upgrade_time += 1
            cool_down_time = 5
        2:
            upgrade_time += 1
            defend = 0.5
        3:
            upgrade_time += 1
            cool_down_time = 3
        4:
            upgrade_time += 1
            Global.max_sheild += 50


func _process(delta: float) -> void:
    if impact_yet == false:
        self.rotation = (get_global_mouse_position() - self.global_position).angle()
    elif impact_yet:
        self.position += impact_direction * delta * 700

func _impact() -> void:
    if cool_down_done:
        cool_down_done = false
        impact_yet = true
        hit_box.remove_from_group("Guardian")
        impact_direction = (get_global_mouse_position() - self.global_position).normalized()
        await get_tree().create_timer(cool_down_time).timeout
        hit_box.add_to_group("Guardian")
        cool_down_done = true
        impact_yet = false
        self.position = Global.core_position
